﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BO.Admin;
using System.Data;
using BO.Admin;
using BO;

namespace ProjectQuaDep.Admin.News
{
    public partial class NewsList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                showListMenuLv1();
                setSubMenu();
                showAll();
                showListMenuLv1Find();
            }
        }
        protected void showAll()
        {
            string sFind = txtSearch.Text.Trim();
            string sValues = rbCheckFind.SelectedValue.ToString();
            string sZoneID="0";
            if (sValues == "1")
            {
                //tim kiem thuong
                sZoneID = "0";
            }
            else
            { 
                //tim nang cao
                sZoneID=ddlZoneFind.SelectedValue.ToString();
            }
            gvShow.DataSource = NewsBO.getAllNewsBO(sFind, sZoneID);
            gvShow.DataBind();
        }
        protected void showListMenuLv1()
        {
            ddlZone.DataSource = NewsBO.getAllZoneCache();
            ddlZone.DataTextField = BO.Const.tbZoneNews.title;
            ddlZone.DataValueField = BO.Const.tbZoneNews.ZoneId;
            ddlZone.DataBind();
        }
        protected void showListMenuLv1Find()
        {
            ddlZoneFind.DataSource = NewsBO.getAllZoneCache();
            ddlZoneFind.DataTextField = BO.Const.tbZoneNews.title;
            ddlZoneFind.DataValueField = BO.Const.tbZoneNews.ZoneId;
            ddlZoneFind.DataBind();
        }

        protected void cmdUpdate_Click(object sender, EventArgs e)
        {
            string ZoneId = ddlZone.SelectedValue.ToString();
            string ZoneIdOther = "";
            string Title = txtTitle.Text;
            string Sapo = txtDesc.Text;
            string Avatar = txtImages.Text;
            string Author = txtTacGia.Text;
            string IsFocus = Utils.getOneSelect(ckFocus);
            string Url = "";
            string Tags = txtTags.Text;
            string status = Utils.getOneSelect(ckSelect);
            string longContent = txtContent.Text;
            for (int i = 0; i < cblZoneOther.Items.Count; i++)
            {
                //lay ra danh sach thuoc chuyen muc nao
                if (cblZoneOther.Items[i].Selected == true)
                {
                    ZoneIdOther += cblZoneOther.Items[i].Value.ToString() + ",";
                }
            }
            if (cmdUpdate.Text == "Thêm Mới")
            {
                //them moi
                string kq = NewsBO.addNewsBO(ZoneId, ZoneIdOther, Title, Sapo, Avatar, Author, IsFocus, Url, Tags, status, longContent);
                if (kq == "0")
                {
                    lblMs.Text = "Xảy ra lỗi khi thêm mới bạn hãy xem lại!";
                }
                else
                {
                    lblMs.Text = "";
                    showAll();
                    pnUpdate.Visible = false;
                }
            }
            else
            {
                //sua chua
                string kq = NewsBO.EditNewsBO(lblID.Text,ZoneId, ZoneIdOther, Title, Sapo, Avatar, Author, IsFocus, Url, Tags, status, longContent);
                if (kq == "0")
                {
                    lblMs.Text = "Xảy ra lỗi khi cập nhật bạn hãy xem lại!";
                }
                else
                {
                    lblMs.Text = "";
                    showAll();
                    pnUpdate.Visible = false;
                }
            }
        }

        protected void cmdHide_Click(object sender, EventArgs e)
        {
            pnUpdate.Visible = false;
        }

        protected void gvShow_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvShow.PageIndex = e.NewPageIndex;
            lblPage.Text = "Trang: " + (gvShow.PageIndex + 1).ToString();
            showAll();
        }

        protected void gvShow_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string cmdName = e.CommandName.ToString();
            string ID = e.CommandArgument.ToString();
            if (cmdName == "cmdEdit")
            {
                DataTable dt = NewsBO.GetInfomationNewsBO(ID);
                if (dt.Rows.Count > 0)
                {
                    cmdUpdate.Text = "Sửa Chữa";
                    pnUpdate.Visible = true;
                    lblID.Text = ID;
                    txtContent.Text = dt.Rows[0][Const.tbNews.longContent].ToString();
                    txtDesc.Text = dt.Rows[0][Const.tbNews.Sapo].ToString();
                    txtImages.Text = dt.Rows[0][Const.tbNews.Avatar].ToString();
                    txtTacGia.Text = dt.Rows[0][Const.tbNews.Author].ToString();
                    txtTags.Text = dt.Rows[0][Const.tbNews.Tags].ToString();
                    txtTitle.Text = dt.Rows[0][Const.tbNews.Title].ToString();
                    Utils.setOneSelect(ckFocus, dt.Rows[0][Const.tbNews.IsFocus].ToString());
                    Utils.setOneSelect(ckSelect, dt.Rows[0][Const.tbNews.status].ToString());
                    ddlZone.SelectedValue = dt.Rows[0][Const.tbNews.ZoneId].ToString();
                    string OtherCat=dt.Rows[0][Const.tbNews.ZoneIdOther].ToString();
                    setSubMenu();
                    for (int i = 0; i < cblZoneOther.Items.Count; i++)
                    {
                        //lay ra danh sach thuoc chuyen muc nao
                        //if (cblZoneOther.SelectedItem.Selected == true)
                        //{
                            string sValues= cblZoneOther.Items[i].Value.ToString()+",";//dinh dang la cat, vd 12,
                            if (OtherCat.Contains(sValues))
                            {
                                cblZoneOther.Items[i].Selected = true;
                            }
                        //}
                    }
                }
            }
        }

        protected void cmdSearch_Click(object sender, ImageClickEventArgs e)
        {
            showAll();
        }

        protected void cmdAdd_Click(object sender, ImageClickEventArgs e)
        {
            pnUpdate.Visible = true;
            cmdUpdate.Text = "Thêm Mới";
            RsControl();
        }

        protected void ddlZone_SelectedIndexChanged(object sender, EventArgs e)
        {
            setSubMenu();
        }
        protected void RsControl()
        {
            lblID.Text = "";
            txtContent.Text = "";
            txtDesc.Text = "";
            txtImages.Text = "";
            txtTacGia.Text = "";
            txtTags.Text = "";
            txtTitle.Text = "";
            ckFocus.Checked = false;
            ckSelect.Checked = false;

        }
        protected void setSubMenu()
        {
            DataTable dtZoneSub = new DataTable();
            dtZoneSub = NewsBO.getAllZoneByParentIdCache(ddlZone.SelectedValue.ToString());
            cblZoneOther.DataSource = dtZoneSub;
            cblZoneOther.DataValueField = BO.Const.tbZoneNews.ZoneId;
            cblZoneOther.DataTextField = BO.Const.tbZoneNews.title;
            cblZoneOther.DataBind();
        }

        protected void rbCheckFind_SelectedIndexChanged(object sender, EventArgs e)
        {
            string sValues = rbCheckFind.SelectedValue.ToString();
            if (sValues == "1")
            {
                pnFind.Visible = false;
            }
            else
            {
                //hien
                pnFind.Visible = true;
            }
        }
    }
}